package frontend.pages.worklog.monthly;

import frontend.model.admin.CalendarDayUIModel;
import frontend.pages.worklog.daily.DailyWorkLogPage;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.*;
import org.apache.wicket.request.resource.ContextRelativeResource;

public class CalendarDayPanel extends Panel {

    private CalendarDayUIModel model;

    public CalendarDayPanel(String id, CalendarDayUIModel model) {
        super(id, new CompoundPropertyModel<CalendarDayUIModel>(model));
        setOutputMarkupId(true);
        this.model = model;

        initComponent();
    }

    private void initComponent() {
        Form dayForm = new Form("dayForm");
        Label label = new Label("dayOfMonth", new PropertyModel<ComponentPropertyModel>(model, "label"));
        ImageButton goToDetailsButton = new ImageButton("detailsButton", new ContextRelativeResource("images/icon_set/16/Write2.png")) {
            @Override
            public void onSubmit() {
                setResponsePage(new DailyWorkLogPage(model.getEmployee(), model.getDate()));
            }
        };
        goToDetailsButton.add(AttributeModifier.replace("title", new ResourceModel("pages.monthlyWorkLogPage.goToDailyView")));
        WebMarkupContainer container = new WebMarkupContainer("container");

        if (model.getWorkLogList().size() > 0) {
            if (model.getHoursTotal() > 0) {
                Label normalHours = new Label("line1", "Hours: " + model.getHoursTotal());
                normalHours.add(AttributeModifier.replace("class", "normalHoursLabelStyle"));
                container.add(normalHours);
            } else {
                Label dayOffLabel = new Label("line1", "Day Off: " + model.getWorkLogList().get(0).getProject().getDisplayLabel());
                dayOffLabel.add(AttributeModifier.replace("class", "dayOffLabelStyle"));
                container.add(dayOffLabel);
            }
        } else {
            container.add(new Label("line1", ""));
        }

        dayForm.add(label, goToDetailsButton, container);
        add(dayForm);
    }

    public CalendarDayUIModel getModel() {
        return model;
    }
}
